package lvcy.message.sender.builder.impl;

import java.lang.reflect.Constructor;


import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import lvcy.message.entity.RequestEntity;
import lvcy.message.sender.HytMessageSender;
import lvcy.message.sender.MessageSender;
import lvcy.message.sender.builder.HytMessageSenderBuilder;

public class HytVarMessageSenderBuilder extends HytMessageSenderBuilder{

	private Logger log=LogManager.getLogger(this);
	public MessageSender build(RequestEntity request) {
		String postData=getPostData(request);
		try {
			Class<?> clazz=Class.forName(HytMessageSender.class.getName());
			Constructor<?> constructor=clazz.getDeclaredConstructor(String.class,String.class);
			this.sender=(HytMessageSender)constructor.newInstance("",postData);
		} catch (Exception e) {
			log.error(e);
		}
		return this.sender;
	}
	@Override
	protected String getPostData(RequestEntity request){
		
		StringBuilder builder=new StringBuilder(super.getPostData(request));
		builder.append("msg=").append(request.getContent()).append("&");
		if(StringUtils.isEmpty(request.getExtras().get("params").toString())){
			throw new IllegalArgumentException("the params must not be null");
		}
		builder.append("params=").append(request.getExtras().get("params").toString());
		return builder.toString();
	}
}
